package ini

import (
	"gopkg.in/ini.v1"
	"log"
	"runtime"
)

var (
	cfg *Config
)

func CfgGetMe() *Config {
	return cfg
}

type Server struct {
	GatewayTcpIp        string
	GatewayTcpPort      int
	GatewayHttpIp       string
	GatewayHttpPort     int
	InnerGatewayTcpIp   string
	InnerGatewayTcpPort int
	InnerOnlineTcpIp    string
	InnerOnlineTcpPort  int
	OnlineHttpIp        string
	OnlineHttpPort      int
	InnerPvpTcpIp       string
	InnerPvpTcpPort     int
	PvpHttpIp           string
	PvpHttpPort         int
	CenterTcpIp         string
	CenterTcpPort       int
	CenterHttpIp        string
	CenterHttpPort      int
	FrameKcpIp          string
	FrameKcpPort        int
	FrameHttpIp         string
	FrameHttpPort       int
}

type Redis struct {
	RedisUrl      string
	RedisPoolSize int
}
type Mongo struct {
	MongoUrl      string
	MongoName     string
	MongoPoolSize int
}

type Nsq struct {
	NsqLookupAddr string
	NsqdAddr      string
}

type Config struct {
	Server *Server
	Mongo  *Mongo
	Redis  *Redis
	Nsq    *Nsq
}

func newConfig() *Config {
	return &Config{
		Server: &Server{},
		Mongo:  &Mongo{},
		Redis:  &Redis{},
		Nsq:    &Nsq{},
	}
}

func LoadCfg(fileName string) {
	cfg = newConfig()
	var path string
	system := runtime.GOOS
	switch system {
	case "windows":
		path = "./config/"
	case "linux":
		path = "../config/"
	}
	conf, err := ini.Load(path + fileName)
	if err != nil {
		log.Printf("load ini", err)
		return
	}
	online := conf.Section("Online")
	if online != nil {
		cfg.Server.InnerOnlineTcpIp = online.Key("InnerTcpIp").String()
		cfg.Server.InnerOnlineTcpPort, _ = online.Key("InnerTcpPort").Int()
		cfg.Server.OnlineHttpIp = online.Key("HttpIp").String()
		cfg.Server.OnlineHttpPort, _ = online.Key("HttpPort").Int()
	}
	pvp := conf.Section("Pvp")
	if pvp != nil {
		cfg.Server.InnerPvpTcpIp = pvp.Key("InnerTcpIp").String()
		cfg.Server.InnerPvpTcpPort, _ = pvp.Key("InnerTcpPort").Int()
		cfg.Server.PvpHttpIp = pvp.Key("HttpIp").String()
		cfg.Server.PvpHttpPort, _ = pvp.Key("HttpPort").Int()
	}
	gateway := conf.Section("Gateway")
	if gateway != nil {
		cfg.Server.GatewayTcpPort, _ = gateway.Key("TcpPort").Int()
		cfg.Server.InnerGatewayTcpIp = gateway.Key("InnerTcpIp").String()
		cfg.Server.InnerGatewayTcpPort, _ = gateway.Key("InnerTcpPort").Int()
		cfg.Server.GatewayHttpIp = gateway.Key("HttpIp").String()
		cfg.Server.GatewayHttpPort, _ = gateway.Key("HttpPort").Int()
	}
	center := conf.Section("Center")
	if center != nil {
		cfg.Server.CenterHttpIp = center.Key("HttpIp").String()
		cfg.Server.CenterHttpPort, _ = center.Key("HttpPort").Int()
		cfg.Server.CenterTcpIp = center.Key("TcpIp").String()
		cfg.Server.CenterTcpPort, _ = center.Key("TcpPort").Int()
	}

	frame := conf.Section("Frame")
	if frame != nil {
		cfg.Server.FrameKcpIp = frame.Key("KcpIp").String()
		cfg.Server.FrameKcpPort, _ = frame.Key("KcpPort").Int()
		cfg.Server.FrameHttpIp = frame.Key("HttpIp").String()
		cfg.Server.FrameHttpPort, _ = frame.Key("HttpPort").Int()
	}

	redis := conf.Section("Redis")
	mongo := conf.Section("Mongo")
	nsq := conf.Section("Nsq")
	if redis == nil || mongo == nil || nsq == nil {
		return
	}
	cfg.Redis.RedisPoolSize, _ = redis.Key("PoolSize").Int()
	cfg.Mongo.MongoName = mongo.Key("MongoName").String()
	cfg.Mongo.MongoPoolSize, _ = mongo.Key("PoolSize").Int()
	switch system {
	case "windows":
		cfg.Redis.RedisUrl = redis.Key("Redis_Addr_Win").String()
		cfg.Mongo.MongoUrl = mongo.Key("Mongo_Addr_Win").String()
		cfg.Nsq.NsqLookupAddr = nsq.Key("NsqdLookup_Win").String()
		cfg.Server.GatewayTcpIp = gateway.Key("TcpIpWin").String()
		cfg.Nsq.NsqdAddr = nsq.Key("Nsqd_Win").String()
	case "linux":
		cfg.Redis.RedisUrl = redis.Key("Redis_Addr_Linux").String()
		cfg.Mongo.MongoUrl = mongo.Key("Mongo_Addr_Linux").String()
		cfg.Server.GatewayTcpIp = gateway.Key("TcpIpLinux").String()
		cfg.Nsq.NsqLookupAddr = nsq.Key("NsqdLookup_Linux").String()
		cfg.Nsq.NsqdAddr = nsq.Key("Nsqd_Linux").String()
	}
}
